
import Common from './common'
export default class Music extends Laya.Script {

    constructor() {
        super();
        this.commonBtnEvent()
    }

    commonBtnEvent(){
        Laya.stage.on("playSound",this,this.playSound)
        Laya.stage.on("playMusic",this,this.playMusic)
        Laya.stage.on("stopMusic",this,this.stopMusic)
    }
    /**
     * 播放音效
     */
    public playSound(source:any) {
        if (Common.getCacheItem("musicSetting") == "1" || Common.getCacheItem("musicSetting") == null) {
            let currentVolume = Laya.SoundManager.musicVolume;
            //console.log
            // Laya.SoundManager.setMusicVolume(currentVolume > 0.4 ? 0.2 : 0.1);//临时将音乐关小
            Laya.SoundManager.playSound(source, 1, Laya.Handler.create(this, () => {
                Laya.SoundManager.setMusicVolume(currentVolume);
            }));
        }
    }

    /**
     * 播放音乐
     */
    public playMusic(source:any,change:boolean) {
        if (Common.getCacheItem("musicSetting") == "1" || Common.getCacheItem("musicSetting") == null) {
            if (Common.musicChannel == null || change) {
                Common.musicChannel = Laya.SoundManager.playMusic(source, 0);
                Laya.SoundManager.autoStopMusic = false;
                Common.setCacheItem("musicSetting", "1")
            } else {
                Common.musicChannel.play();
                Common.setCacheItem("musicSetting", "1")
            }
        }

    }

    /**
     * 停止背景音乐
     */
    public stopMusic() {
        Laya.SoundManager.stopMusic();
        Common.setCacheItem("musicSetting", "0")
    }
}